﻿package 
{
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.display.StageQuality;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import org.papervision3d.core.render.sort.BasicRenderSorter;
	import org.papervision3d.materials.BitmapFileMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.primitives.Cube;
	import org.papervision3d.objects.primitives.Sphere;
	//pv3d 加载PV3D引擎
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.materials.MovieMaterial;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.render.QuadrantRenderEngine;
	import org.papervision3d.view.Viewport3D;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.papervision3d.core.utils.virtualmouse.*;
	import org.papervision3d.core.utils.*;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.objects.primitives.Plane;
	
	/**
	* ...
	* @author ezshine
	*/
	public class main extends Sprite 
	{
		private var scene:Scene3D; //创建一个三维场景
		private var renderer:QuadrantRenderEngine ;//创建一个渲染引擎
		private var viewport:Viewport3D ;//创建一个视图
		private var camera :Camera3D;//创建一个摄像机
		private var vMouse:VirtualMouse;
		
		private var xmlConfigloader:URLLoader;
		private var pic_cfg:XML;
		
		private var planeContainer:DisplayObject3D;
		
		public function main()
		{
			setupStage();
			
			xmlConfigloader = new URLLoader();
			xmlConfigloader.addEventListener(Event.COMPLETE, parseXML);
			
			var request:URLRequest = new URLRequest("pic_cfg.xml");
			xmlConfigloader.load(request);
		}
		
		public function setupStage()
		{
			stage.align = StageAlign.TOP_LEFT;
			stage.frameRate = 60;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.quality = StageQuality.HIGH;
		}
		
		public function setupPapervision()
		{
			viewport = new Viewport3D(stage.stageWidth, stage.stageHeight, true, true, true);
			
			addChild(viewport);
			
			renderer = new QuadrantRenderEngine();
			
			scene = new Scene3D();
			
			camera = new Camera3D();
			
			camera.zoom = 100;
			
			planeContainer = new DisplayObject3D("planeContainer");
			
			var layerNum:Number = pic_cfg.children().length();
			
			for (var i:Number = 0; i < layerNum; i++)
			{
				var src:String = pic_cfg.children()[i].attribute("src");
				var w:Number = pic_cfg.children()[i].attribute("w");
				var h:Number = pic_cfg.children()[i].attribute("h");
				var x:Number = pic_cfg.children()[i].attribute("x");
				var y:Number = pic_cfg.children()[i].attribute("y");
				var z:Number = pic_cfg.children()[i].attribute("z");
				var layerPlane:Plane = new Plane(new BitmapFileMaterial(src), w, h, 3, 3);
				layerPlane.x = x;
				layerPlane.y = y;
				layerPlane.z = z;
				
				planeContainer.addChild(layerPlane);
			}
			
			var boxMaterial:MaterialsList = new MaterialsList();
			boxMaterial.addMaterial(new BitmapFileMaterial(pic_cfg.attribute("topTexture")), "top");
			boxMaterial.addMaterial(new BitmapFileMaterial(pic_cfg.attribute("bottomBox")), "bottom");
			boxMaterial.addMaterial(new BitmapFileMaterial(pic_cfg.attribute("bottomBox")), "front");
			boxMaterial.addMaterial(new BitmapFileMaterial(pic_cfg.attribute("bottomBox")), "back");
			boxMaterial.addMaterial(new BitmapFileMaterial(pic_cfg.attribute("bottomBox")), "left");
			boxMaterial.addMaterial(new BitmapFileMaterial(pic_cfg.attribute("bottomBox")), "right");
			
			var bottomBox:Cube = new Cube(boxMaterial, 996, 600, 600, 6, 3, 3);
			bottomBox.y = -600;
			
			planeContainer.addChild(bottomBox);
			
			scene.addChild(planeContainer);
			
			addEventListener( Event.ENTER_FRAME, loop );
		}
		
		private function loadPhotoXML()
		{
			xmlConfigloader = new URLLoader();
			xmlConfigloader.addEventListener(Event.COMPLETE, parseXML);
			
			var request:URLRequest = new URLRequest("pics_cfg.xml");
			xmlConfigloader.load(request);
		}
		
		private function parseXML(event:Event):void
		{
			xmlConfigloader.removeEventListener(Event.COMPLETE, parseXML);
			pic_cfg = new XML(xmlConfigloader.data);
			
			setupPapervision();
		}
		
		public function loop(e:Event)
		{
			planeContainer.rotationY = (mouseX-stage.width/2)/20;
			planeContainer.rotationX = (mouseY-stage.height/2)/20;
			renderer.renderScene(scene,camera,viewport);
		}
	}
	
}